﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Calendar;

namespace SportManager
{
    class GoogleCalendarManager
    {
        public GoogleCalendarManager(Options options, string userName, string password)
        {
            SportOptions = options;
            UserName = userName;
            Password = password;
        }

        public bool IsCalendarExists()
        {
            CalendarService service = GetCalendarService();
            CalendarQuery calendarEvent = new CalendarQuery(OwnCalendars);
            CalendarFeed calendars = service.Query(calendarEvent);

            while (calendars != null)
            {
                foreach (CalendarEntry entry in calendars.Entries)
                {
                    if (entry.Title.Text == CalendarName)
                    {
                        return true;
                    }
                }

                if (calendars.NextChunk != null)
                {
                    calendarEvent.Uri = new Uri(calendars.NextChunk);
                    calendars = service.Query(calendarEvent) as CalendarFeed;
                    continue;
                }

                break;
            }

            return false;
        }

        public void CreateCalendar()
        {
            CalendarService service = GetCalendarService();
            CalendarEntry calendar = new CalendarEntry();
            calendar.Title.Text = CalendarName;
            calendar.Summary.Text = CalendarDescription;
            calendar.TimeZone = "Europe/Moscow";
            calendar.Hidden = false;
            calendar.Authors.Add(new AtomPerson(AtomPersonType.Author, "STD"));
            calendar.Color = "#2952A3";
            calendar.Location = new Where("empty", "empty", "Russia");
            Uri postUri = new Uri(OwnCalendars);
            CalendarEntry createdCalendar = (CalendarEntry)service.Insert(postUri, calendar);
            SportOptions.GoogleCalendarPath = createdCalendar.SelfUri.Content.ToString();
        }

        public void TestGettingEvents()//temp function
        {
            CalendarService service = GetCalendarService();
            string calendarURI = "https://www.google.com/calendar/feeds/alexey.sakharov@gmail.com/private/full";

            EventQuery queryEvent = new EventQuery();
            queryEvent.Uri = new Uri(calendarURI);
            queryEvent.StartDate = DateTime.Now.AddMonths(-1);
            queryEvent.EndTime = DateTime.Now.AddMonths(1);
            queryEvent.NumberToRetrieve = 100;
            
            EventFeed events = service.Query(queryEvent) as EventFeed;

            ArrayList dates = new ArrayList();
            while (events != null)
            {
                foreach (EventEntry entry in events.Entries)
                {
                    if (entry.Times.Count > 0)
                    {
                        foreach (When w in entry.Times)
                        {
                            dates.Add(w.StartTime);
                        }
                    }
                }

                if (events.NextChunk != null)
                {
                    queryEvent.Uri = new Uri(events.NextChunk);
                    events = service.Query(queryEvent) as EventFeed;
                    continue;
                }

                break;
            }                
        }

        private CalendarService GetCalendarService()
        {
            CalendarService service = new CalendarService(ProgramNameForGoogle);
            service.setUserCredentials(UserName, Password);
            return service;
        }

        private Options SportOptions;
        private const string OwnCalendars = "https://www.google.com/calendar/feeds/default/owncalendars/full";
        private const string ProgramNameForGoogle = "SportManager";
        private const string CalendarName = "Sport Training Diary calendar";
        private const string CalendarDescription = "Этот календарь содержит список тренеровок.";
        private string UserName, Password;
    }
}
